package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.PriorityQueue;

/*
ID:		liluyan1
LANG:	JAVA
TASK:	comehome
*/
public class comehome {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("comehome.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"comehome.out")));
		new comehome().solve(f, out);
		out.close();
		System.exit(0);
	}
	
	private final static int INF = 52001;
	
	private void solve(BufferedReader f, PrintWriter out) throws IOException {
		int N = read(f);
		int M = 52;
		int[][] g = new int[M][M];
		for (int i=0; i<M; i++) {
			for (int j=0; j<M; j++) {
				g[i][j] = INF;
			}
		}
		
		int s = 'Z' - 'A';
		HashSet<Integer> q = new HashSet<Integer>();
		
		for (int i=0; i<N; i++) {
			String[] t = f.readLine().split(" ");
			int p1 = toNum(t[0].charAt(0));
			int p2 = toNum(t[1].charAt(0));
			q.add(p1);
			q.add(p2);
			int d = Integer.parseInt(t[2]);
			g[p1][p2] = Math.min(g[p1][p2], d);
			g[p2][p1] = Math.min(g[p2][p1], d);
		}
		
		int[] weight = new int[M];
		for (int i=0; i<M; i++) weight[i] = g[s][i];
		
		while (!q.isEmpty()) {
			int min = INF;
			int index = 0;
			for (int i : q) {
				if (weight[i] < min) {
					min = weight[i];
					index = i;
				}
			}
			
			if (index >= 0 && index < 25) {
				out.print((char)('A' + index));
				out.println(" " + weight[index]);
				return;
			}
			
			q.remove(index);
			for (int i=0; i<M; i++) {
				weight[i] = Math.min(weight[i], weight[index] + g[index][i]);
			}
		}
	}
	
	private int toNum(char c) {
		return c>='A' && c<='Z'? c-'A' : c-'a'+26;
	}
	
	public int read(BufferedReader f) throws IOException {
		return Integer.parseInt(f.readLine());
	}
	
	public int[] read(BufferedReader f, int N) throws IOException {
		String[] t = f.readLine().split(" ");
		int[] a = new int[N];
		for (int i=0; i<N; i++) {
			a[i] = Integer.parseInt(t[i]);
		}
		return a;
	}
}
